﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace com.Bliss.Domain.User
{
    class UserDbContext : DbContext
    {
        public UserDbContext()
            : base("BlissConString")
        {

        }
        public DbSet<Bliss.User> Users { get; set; }
        public DbSet<Bliss.Group> Groups { get; set; }
        public DbSet<Bliss.Role> Roles { get; set; }
        //public DbSet<Bliss.UserGroup> UserGroups { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
          modelBuilder.Entity<Bliss.Role>()
            .HasMany(p => p.Users)
            .WithMany(t => t.Roles)
            .Map(mc =>
            {
                mc.ToTable("UserRoles");
                mc.MapLeftKey("RoleId");
                mc.MapRightKey("UserId");
            });

          modelBuilder.Entity<Bliss.User>()
          .HasMany(p => p.Groups)
          .WithMany(t => t.Users)
          .Map(mc =>
          {
              mc.ToTable("UserGroups");
              mc.MapLeftKey("GroupId");
              mc.MapRightKey("UserId");
          });

            //modelBuilder.Entity<Bliss.User>()
            //    .HasMany(u=>u.Groups)
            //    .WithMany(u=>u.Users)
            //    .Map(mc =>
            //      {
            //          mc.ToTable("UserGroups");
            //          mc.MapLeftKey("GroupId");
            //          mc.MapRightKey("UserId");
            //      });
        }
    }
}
